#ifndef CL_File_H //Header guard
#define CL_File_H //Header guard

#include <boost/shared_ptr.hpp>
#include <string>
#include <fstream>

/*********

File object. An interface that controls binary/text mode, 
reading and writing POD data, and some other properties.
Can be used as a base class for specific simple files.

*********/
enum E_FileModes
{
	E_FileModes_ReadText,
	E_FileModes_WriteText,
	E_FileModes_ReadBinary,
	E_FileModes_WriteBinary,
	E_FileModes_None,
	E_FileModes_Count
};

class CL_File
{

public:

	CL_File();
	~CL_File();

	static CL_File*		Open(const std::string & filename, E_FileModes filemode);
	void				Close();
	void				SwitchFilemode(E_FileModes filemode);
	

	std::string 		GetFilename();
	void				SetFilename(std::string filename);
	E_FileModes			GetFilemode();
	bool				IsOpen();
	bool				IsEOF();
	
	//**********Binary I/O***********/
	void				ReadInt(int &);
	void				ReadFloat(float &);
	void				ReadDouble(double &);
	void				ReadChar(char &);
	void				ReadString(std::string &, int length);
	void				ReadStringLengthPrefix(std::string &);

	void				WriteInt(int buffer);
	void				WriteFloat(float buffer);
	void				WriteDouble(double buffer);
	void				WriteChar(char buffer);
	void				WriteString(const std::string & buffer);
	void				WriteStringLengthPrefix(const std::string & buffer);


	//***********Text I/O***********/
	void ReadLine(std::string & line);
	void ReadLineWithLength(std::string & line, int length);
	void WriteLine(const std::string &);



private:
	static bool							CheckFileExists(const std::string & filename);
	static std::string					ObtainCFilemode(E_FileModes filemode, bool isOpen, const std::string & filename); //Gets the C flag i.e "r", "w", etc.

	FILE*								m_pFile;
	E_FileModes							m_filemode;
	std::string							m_sFilename;
	int									m_uLength;
	bool								m_bIsOpen;
	bool								m_bEof;

};

#endif //CL_File_H Header guard